package com.itheima.web.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.cargo.*;
import com.itheima.service.cargo.*;
import com.itheima.utils.DownloadUtil;
import com.itheima.web.controller.BaseController;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/cargo/finance")
public class FinanceController extends BaseController {

    @Reference
    private ShippingOrderService shippingOrderService;
    @Reference
    private InvoiceService invoiceService;
    @Reference
    private PackingService packingService;
    @Reference
    private FinanceService financeService;
    @Reference
    private ExportProductService exportProductService;
    @RequestMapping(value = "/list", name = "发票列表查询")
    public String list(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        FinanceExample financeExample = new FinanceExample();
        PageInfo pageInfo = financeService.findByPage(pageNum, pageSize, financeExample);
        request.setAttribute("page",pageInfo);
        return "cargo/finance/finance-list";
    }
    @RequestMapping(value = "/toFinance",name = "跳转生成财务报表页面")
    public String  toInvoice(String id){
        Invoice invoice = invoiceService.findById(id);
        request.setAttribute("invoice",invoice);
        ShippingOrder shippingOrder = shippingOrderService.findById(id);
        request.setAttribute("shippingOrder",shippingOrder);
        Packing packing = packingService.findById(id);
        request.setAttribute("packing",packing);
        return "cargo/finance/finance-tofinance";
    }
    @RequestMapping(value = "/toView", name = "跳转查看财务报运单页面")
    public String toView(String id) {
        Finance finance = financeService.findById(id);
        request.setAttribute("finance",finance);
        return "cargo/finance/finance-view";
    }

    @RequestMapping(value = "/edit",name = "生成财务报表页面或修改")
    public String  edit(String id, Finance finance){
        //制单时间为空,自动生成
        if (finance.getInputDate() == null){
            finance.setInputDate(new Date());
        }
        finance.setInvoiceId(id);
        //制单人生成
        finance.setInputBy(getLoginUser().getUserName());
        //状态
        finance.setState(0);
        //创建人信息
        finance.setCreateBy(getLoginUser().getUserName());
        finance.setCreateDept(getLoginUser().getDeptName());
        finance.setCreateTime(new Date());
        financeService.save(finance);
        //修改发票状态
        Invoice invoice = invoiceService.findById(id);
        invoice.setState(1);
        invoiceService.update(invoice);
        return "redirect:/cargo/finance/list.do";
    }
    @RequestMapping(value = "/delete", name = "财务报表单删除")
    public String delete(String id) {
        financeService.delete(id);
        return "redirect:/cargo/finance/list.do";
    }
    @RequestMapping(value = "/financeExcel", name = "导出财务报表单")
    public void financeExcel(String id) throws IOException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Finance finance = financeService.findById(id);
        String invoiceId = finance.getInvoiceId();
        Packing packing = packingService.findById(invoiceId);
        ShippingOrder shippingOrder = shippingOrderService.findById(invoiceId);
        Invoice invoice = invoiceService.findById(invoiceId);
        //读取模板
        String realPath = session.getServletContext().getRealPath("make/xlsprint/tFINANCE.xls");
        Workbook workbook = new HSSFWorkbook(new FileInputStream(new File(realPath)));
        Sheet sheet = workbook.getSheetAt(0);
        Row row1 = sheet.getRow(1);
        String inputDate = format.format(finance.getInputDate());
        row1.createCell(3).setCellValue(inputDate);
        row1.createCell(15).setCellValue(packing.getInvoiceNo());
        Row row3 = sheet.getRow(3);
        row3.createCell(4).setCellValue(packing.getBuyer());
        Row row4 = sheet.getRow(4);
        row4.createCell(2).setCellValue(shippingOrder.getPortOfLoading());
        row4.createCell(5).setCellValue(shippingOrder.getPortOfLoading());
        row4.createCell(10).setCellValue(invoice.getTradeTerms());
        String exportIds = packing.getExportIds();
        ExportProductExample exportProductExample = new ExportProductExample();
        String[] split = exportIds.split(",");
        List<String> list = Arrays.asList(split);
        int n = 7;
        for (String exportId : list) {
            List<ExportProduct> exportProductList = exportProductService.findByExportId(exportId);
            for (ExportProduct exportProduct : exportProductList) {
                Row rown = sheet.createRow(n++);
                rown.createCell(1).setCellValue(exportProduct.getProductNo());
                rown.createCell(4).setCellValue(exportProduct.getFactoryName());
                rown.createCell(5).setCellValue("PCS");
                rown.createCell(6).setCellValue(exportProduct.getCnumber());
                rown.createCell(7).setCellValue(exportProduct.getBoxNum());
                rown.createCell(8).setCellValue(exportProduct.getGrossWeight());
                rown.createCell(9).setCellValue(exportProduct.getNetWeight());
                rown.createCell(10).setCellValue(exportProduct.getSizeLength());
                rown.createCell(11).setCellValue(exportProduct.getSizeWidth());
                rown.createCell(12).setCellValue(exportProduct.getSizeHeight());
                rown.createCell(13).setCellValue(exportProduct.getExPrice());
                double invMoney = exportProduct.getExPrice() * exportProduct.getCnumber() * exportProduct.getBoxNum();
                rown.createCell(14).setCellValue(invMoney);
                rown.createCell(15).setCellValue(exportProduct.getTax());
            }
        }
        //下载
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        workbook.write(byteArrayOutputStream);
        DownloadUtil.download(byteArrayOutputStream,response,"发票表.xls");
    }

}
